﻿using ArtMath.Component.Point;
using ArtMath.Data;
using ArtMath.Resource;
using System;
using System.ComponentModel;
using ArtMath.CommonType;

namespace ArtMath.Component.Manager
{
    /// <summary>
    /// 组件管理器基类
    /// </summary>
    [Serializable]
    public abstract class ComponentManagerBase : ShapeBase
    {
        public ComponentManagerBase(SketchPad pad, string name, ToolTypeName toolType, bool addToList = true) : base(pad, name, toolType, addToList)
        {

        }
        [Browsable(false)]
        public override RPointS RotateCenter
        {
            get
            {
                return base.RotateCenter;
            }

            set
            {
                base.RotateCenter = value;
            }
        }
        [Browsable(false)]
        public override AngleStringX RotateAngle
        {
            get
            {
                return base.RotateAngle;
            }

            set
            {
                base.RotateAngle = value;
            }
        }
        public override bool IsIndependent
        {
            get
            {
                return false;
            }
        }
        public override bool IsEntity
        {
            get
            {
                return false;
            }
        }
        protected override bool DoAngleFix
        {
            get
            {
                return false;
            }
        }
        public override bool CanRecover
        {
            get
            {
                if (!base.CanRecover) return false;
                bool can = true;
                ForEachManagedComponent((Base obj) =>
                {
                    if (obj.ObjectState != ObjectState.Normal)
                    {
                        can = false;
                    }
                    return can;
                });
                return can;
            }
        }
        //public virtual void PointMove_Prepare(PointBase movePoint)
        //{

        //}
        //public virtual void PointMoved_Clean(PointBase movePoint)
        //{

        //}
        public virtual void PointMoving_ManagedComponentsLimit(Base component)
        {

        }
        protected virtual void ForEachManagedComponent(Predicate<Base> act)
        {

        }
        public override void Relocate(PointsRelocationReason reason)
        {
            InChildrenIterator((ShapeBase sb) =>
            {
                sb.Relocate(reason);
                return true;
            });
            base.Relocate(reason);
        }
        public override void Recover()
        {
            ForEachManagedComponent((Base obj) =>
            {
                if (obj.ObjectState != ObjectState.Normal)
                {
                    //尝试恢复obj
                    if (obj.CanRecover)
                    {
                        try
                        {
                            obj.Recover();
                            return true;
                        }
                        catch (Exception)
                        {

                        }
                    }
                    throw new ObjectDisposedException(obj.Name, string.Format(Resources.Translate("CannotFindObject"), obj.Name));
                }
                return true;
            });
            base.Recover();
        }
    }
}
